import { createRouter, createWebHistory } from 'vue-router'
import store from "../store";
import {request} from "../axios";

const routes = [
  {
    name:'Main',
    path:'/',
    component: ()=>import('../views/Main'),
    children: [
      {
        path: '/',
        redirect:'/home'
      },
      {
        name:'Home',
        path:'/home',
        component: ()=>import('../views/body/PageBody'),
      },
      {
        name:'Category',
        path:'/category',
        component:()=>import('../views/body/category/Category'),
        children:[
          {
            path:':tagname',
            components:{
              body:()=>import('../views/body/content/ContentBody'),
              footer:()=>import('../views/common/LimitControl')
            }
          }
        ]
      },
      {
        name:'Message',
        path:'/message',
        component:()=>import('../views/body/message/Message')
      },
      {
        name:'DataRequest',
        path: '/articledetails/datarequest/:id',
        component:()=>import('../views/body/content/DataRequest')
      },
      {
        name:'Articledetails',
        path:'/articledetails/:id',
        component:()=>import('../views/body/content/ArticleDetails')
      }
    ]
  },
  {
    name:'AdminPage',
    path:'/adminpage',
    component:()=>import('../views/admin/adminhome/AdminHome'),
    meta:{
      requireAuth:true
    },
    children: [
      {
        path: '',
        redirect: '/adminpage/blogm/showblog',
        meta:{
          requireAuth:true
        },
      },
      {
        name:'BlogM',
        path: '/adminpage/blogm',
        component:()=>import('../views/admin/blogManagement/BlogManagement'),
        children: [
          {
            path: '',
            redirect: '/adminpage/blogm/showblog',
            meta:{
              requireAuth:true
            },
          },
          {
            name:'EditBlog',
            path:'/adminpage/blogm/editblog',
            component:()=>import('../views/admin/blogManagement/BlogEdit'),
            meta:{
              requireAuth:true
            },
          },
          {
            name:'ShowBlog',
            path:'/adminpage/blogm/showblog',
            component:()=>import('../views/admin/blogManagement/BlogDetails'),
            meta:{
              requireAuth:true
            }
          },
        ]
      },
      {
        name:'CategoryM',
        path: '/adminpage/categorym',
        component:()=>import('../views/admin/CategoryManagement/CategoryManagement'),
        meta:{
          requireAuth:true
        }
      },
      {
        name:'TagM',
        path: '/adminpage/tagm',
        component:()=>import('../views/admin/TagManagement/TagManagement'),
        meta:{
          requireAuth:true
        }
      },
      {
        name:'MessageM',
        path: '/adminpage/messagem',
        component:()=>import('../views/admin/MessageManagement/MessageManagement'),
        meta:{
          requireAuth:true
        }
      },
      {
        name:'AccessInfo',
        path: '/adminpage/accessinfo',
        component:()=>import('../views/admin/AccessInfo/AccessInfo'),
        meta:{
          requireAuth:true
        }
      }
    ],
  },
  {
    name: 'LoginT',
    path: '/tologin',
    component: () => import('../views/common/Login')
  },
]

const router = createRouter({
  history:createWebHistory(process.env.BASE_URL),
  base:'/prblog/',
  routes
})
// router.onError((error) => {
//   const pattern = /Loading chunk (\d)+ failed/g;
//   const isChunkLoadFailed = error.message.match(pattern);
//   const targetPath = router.history.pending.fullPath;
//   if (isChunkLoadFailed) {
//     router.replace(targetPath);
//   }
// });
router.beforeEach((to, from, next) => {
  document.title='prblog | '+to.name
  if(to.meta.requireAuth==true){
    if(store.getters.getToken){
      let token=store.getters.getToken
      request({
        url:'/backend/verify',
        headers: {
          authorization: token,
        },
        method:"POST"
      }).then(res=>{
        if(res.data.status==200){
          next()
        }else{
          next('/tologin')
        }
      }).catch(err=>{
        next('/tologin')
      })
    }else{
      next('/tologin')
    }
  }else{
    next()
  }
})
router.afterEach((to,from,next) => {
  window.scrollTo(0,0);
});

export default router
